package com.huiquan.icd.controller;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.service.IcdDiseaseNationalStdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IcdDiseaseNationalStdController {

	@Autowired
	private IcdDiseaseNationalStdService icdDiseaseNationalStdService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_national_std_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String detailCode = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "detailCodeStr");
		String category = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "category");
		String detailModifyName = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "detailModifyName");

		return icdDiseaseNationalStdService.list(start, codeSearch, exactSearchFlag, countPerPageStr, detailCode, category, detailModifyName);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_national_std_change_disable.do")
	public ReturnData changeDisable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disable = req.getParameter("disable");
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = icdDiseaseNationalStdService.changeDisable(id, user, disable);
		return rtn;
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_national_std_export.do")
	public ReturnData export(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String detailCode = req.getParameter("detailCode");
		String detailAccessoryCode = req.getParameter("detailAccessoryCode");
		String detailName = req.getParameter("detailName");
		String categoryName = req.getParameter("categoryName");
		String disable = req.getParameter("disable");
		String detailModifyName = req.getParameter("detailModifyName");
		String chapterSequence = req.getParameter("chapterSequence");
		String chapterCode = req.getParameter("chapterCode");
		String chapterName = req.getParameter("chapterName");
		String sectionCode = req.getParameter("sectionCode");
		String sectionName = req.getParameter("sectionName");
		String gpCode = req.getParameter("gpCode");
		String gpAccessoryCode = req.getParameter("gpAccessoryCode");
		String gpName = req.getParameter("gpName");
		String suborderCode = req.getParameter("suborderCode");
		String suborderAccessoryCode = req.getParameter("suborderAccessoryCode");
		String suborderName = req.getParameter("suborderName");

		icdDiseaseNationalStdService.export(resp, detailCode, detailAccessoryCode, detailName, categoryName, disable,
				detailModifyName, chapterSequence, chapterCode, chapterName, sectionCode, sectionName, gpCode,
				gpAccessoryCode, gpName, suborderCode, suborderAccessoryCode, suborderName);
		return ReturnUtil.success();
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_national_std_submit_edit.do")
	public ReturnData submitEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String detailModifyName = req.getParameter("detailModifyName");
		String disable = req.getParameter("disable");
		String categoryName = req.getParameter("categoryName") == null ? "" : req.getParameter("categoryName");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");
		return icdDiseaseNationalStdService.submitEdit(id,  disable, categoryName,detailModifyName, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_national_std_import.do")
	public ReturnData synonymImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseNationalStdService.nationalStdImport(file, user);
	}

	@RequestMapping(value = "/icd/disease_national_std_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String id = req.getParameter("id");
		return icdDiseaseNationalStdService.preEdit(id);
	}
}
